<?php namespace Common\Model;
use Hdphp\Model\Model;

class User extends Model{
    protected $table = 'user';

    protected $auto = [
        ['userpassword','md5','function',3,3]
    ];

    protected $validate = [
        ['username','required','用户账号必填',3,3],
        ['userpassword','required','密码必填',3,3],
        ['userpassword','minlen:6','密码不得少于6位',3,3],
        ['code','required','验证码必填',3,3],
        ['code','checkcode','验证码不正确',3,3],
        ['username','checkusername','用户账号已存在',3,3],
    ];

//    验证验证码
    public function checkcode(){
        $code=Q('post.code');
        if(strtolower($code)!=strtolower($_SESSION['code'])){
            return false;
        }else{
            return true;
        }
    }

//    检测用户账号
    public function checkusername(){
        $username=Q('post.username');
        $name=$this->where("username='{$username}'")->get();
        if($name){
            return false;
        }else{
            return true;
        }
    }

    public function store(){
        if(!$this->create()) return false;
        return $this->add();
    }
}